package com.easyshopping;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.easyshopping.domain.Item;
import com.easyshopping.domain.ItemCategory;
import com.easyshopping.facade.ShoppingListFacade;

public class SelectItemActivity extends Activity implements OnClickListener, OnCheckedChangeListener{

	private Button btn_Back;
	private TextView tv_ctName;
	private ListView ls_itemList;
	private Cursor mCursorItems;
	
	private int cat_id;
	private String cat_name;
	
	private boolean[] selected;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.itemslist);
			
		cat_id = ((Integer)this.getIntent().getExtras().get(ItemCategory.ITEMCATEGORY_ID)).intValue();
		cat_name = (String)this.getIntent().getExtras().get(ItemCategory.ITEMCATEGORY_NAME);
		
		btn_Back = (Button) this.findViewById(R.id.Btn_Back_to_Additem);
		btn_Back.setOnClickListener(this);
		
		tv_ctName = (TextView)this.findViewById(R.id.Title_Category_Name);
		tv_ctName.setText(cat_name);
		
		ls_itemList = (ListView)this.findViewById(R.id.Itemlist);
//		ls_itemList.setAdapter(new ItemAdapter(this));
		fillItemsList();
		
	}


	public void onClick(View view) {
		
		if (view.equals(btn_Back)) {
			saveItems();
			this.finish();
			return;
		}
		
	}
	
	private void saveItems() {
		
		int count = mCursorItems.getCount();
		
		for (int i = 0; i < count; i++) {
			
			if (selected[i]) {
				mCursorItems.moveToPosition(i);
				ShoppingListFacade.getInstance().addItem(
						mCursorItems.getString(mCursorItems.getColumnIndex(Item.ITEM_NAME)), cat_id);
			}
		}
	}
	
	private void fillItemsList() {
		
//		mCursorItems = ShoppingListFacade.getInstance().getPersistence().fetchAllItemsOfCategory(cat_id);
//		
//		selected = new  boolean[mCursorItems.getCount()];
//		
//		ls_itemList.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, 
//				mCursorItems, new String[] { Item.ITEM_NAME }, new int[] {android.R.id.text1}));
//		ls_itemList.setItemsCanFocus(false);
//		ls_itemList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//		ls_itemList.setOnItemClickListener(this);
		
		mCursorItems = ShoppingListFacade.getInstance().getPersistence().fetchAllItemsOfCategory(cat_id);
		selected = new  boolean[mCursorItems.getCount()];
		
		startManagingCursor(mCursorItems);
		
		ls_itemList.setAdapter(new ItemsofCategoryCursorAdapter(this,
				mCursorItems));
		
		ls_itemList.setItemsCanFocus(false);
		ls_itemList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}

	private class ItemsofCategoryCursorAdapter extends ResourceCursorAdapter {

		public ItemsofCategoryCursorAdapter(Context context, Cursor c) {
			super(context, R.layout.list_item2select, c);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			LayoutInflater inflator = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			return inflator.inflate(R.layout.list_item2select, parent, false);
		}

		@Override
		public void bindView(View view, Context ctx, Cursor cursor) {
			
			TextView tv_Item = (TextView) view.findViewById(R.id.Item2Select);
			tv_Item.setText(cursor.getString(cursor
					.getColumnIndex(Item.ITEM_NAME)));

			CheckBox cb_state1 = (CheckBox)view.findViewById(R.id.CB_Item2select);
			int item_position = cursor.getPosition();
			cb_state1.setTag(new Integer(item_position));
			cb_state1.setOnCheckedChangeListener(SelectItemActivity.this);
		}
	}
	
	
//	public void onItemClick(AdapterView<?> l, View view, int position, long id) {
//		
//		Log.i("info", "item clicked");
//		
//		selected[position] = !selected[position];
//	}


	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		Log.i("info", "item clicked");
		int item_position = ((Integer)buttonView.getTag()).intValue();
		selected[item_position] = !selected[item_position];
	}
}
